<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11/011
 * Time: 17:33
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class video extends Controller
{
    public function list()//显示视频列表方法
    {
        $this->checkLogin();
        $result=Db::table('video')
            ->alias('a')
            ->join('cub w','a.cub_id = w.cub_id')
            ->select();
        $count = count($result);    //获取俱乐部条数
        $this->assign('count', $count);
        $this->assign('result',$result);
        return $this->fetch();
    }

    public function addlist()//视频添加页面方法
    {
        $this->checkLogin();
        $session=Session::get('admin');
        $cub=Db::table('cub')->where('adminname',$session['adminname'])->select();
        $this->assign('cub',$cub);
        return $this->fetch('addlist');
    }

    public function add()//处理视频添加
    {
        $this->checkLogin();
        $request=input('');
        $pic=request()->file('video_pic');//获取图片
        $video=request()->file('video_address');//获取视频
        $info = $pic->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'.DS.'img');
        $info1 = $video->rule('uniqid')->validate(['size'=>999000000,'ext'=>'mp4'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'.DS.'video');
        if ($info){
            $picname=$info->getFilename();
            $request['video_pic']=$picname;
        }else{
            // 上传失败获取错误信息
            echo $info->getError();
        }
        if ($info1){
            $videoname=$info1->getFilename();
            // dump($videoname);
            $a=strlen($videoname);
            $name=substr($videoname,0,$a-4);
            $namepic=$name.'.mp4';
            $request['video_address']=$namepic;
        }else{
            // 上传失败获取错误信息
            echo $info->getError();
        }
        $insert=Db::table('video')->insert($request);
        if ($insert){
            $this->success('成功','admin/video/list');
        }
        else{
            $this->error('失败','admin/video/list');
        }
    }

    public function delect()
    {
        $this->checkLogin();
        $request=input('video_id');
        $sql=Db::table('video')->where('video_id',$request)->find();
        $pic=$sql["video_pic"];
        $file=ROOT_PATH."public".DS."static".DS. 'admin'.DS.'img'.DS.$pic;
        $pic1=$sql["video_address"];
        $file1=ROOT_PATH."public".DS."static".DS. 'admin'.DS.'video'.DS.$pic1;
        if (file_exists($file)) {
            unlink($file);//删除文件
            if (file_exists($file1)) {
                unlink($file1);//删除文件
            }
        }
        if (Db::table("video")->delete($request)){
            $this->success("删除成功");
        }
        else{
            $this->error("删除失败");
        }
    }
}